﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icst.Leaflike.Logging;

namespace Icst.Leaflike.Cache
{
    public class MemcachedCachePolicy : ICachePolicy
    {
        private readonly MemcachedClient _cache;
        public MemcachedCachePolicy()
        {
            try
            {
                Logger.Info("SETUP MEMCACHED");
                _cache = MemcachedClient.GetInstance("MemcachedConfig");
                _cache.MaxPoolSize = 10000;
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO SETUP MEMCACHED. " + e.Message);
            }
        }
        public void Add<T>(string key, T value)
        {
            try
            {
                _cache.Set(key, value);
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO ADD CACHE ITEM [" + key + "]." + e.Message);
            }
        }
        public void Add<T>(string key, T value, DateTime dt)
        {
            try
            {
                _cache.Set(key, value, dt);
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO ADD CACHE ITEM [" + key + "]." + e.Message);
            }
        }
        public T Get<T>(string key)
        {
            try
            {
                object o = _cache.Get(key);
                return (T)o;
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO GET CACHE ITEM [" + key + "]." + e.Message);
                _cache.Delete(key);
                return default(T);
            }
        }
        public T GetIf<T>(string key, Func<T> getobject)
        {
            T obj = Get<T>(key);
            if (obj == null)
            {
                obj = getobject();
                if (obj != null)
                {
                    Add(key, obj);
                }
            }

            return obj;
        }
        public void Add(string key, object value)
        {
            try
            {
                _cache.Set(key, value);
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO ADD CACHE ITEM [" + key + "]." + e.Message);
            }
        }

        public void Add(string key, object value, DateTime dt)
        {
            try
            {
                _cache.Set(key, value, dt);
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO ADD CACHE ITEM [" + key + "]." + e.Message);
            }
        }

        public object Get(string key)
        {
            try
            {
                return (object)_cache.Get(key);
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO GET CACHE ITEM [" + key + "]." + e.Message);
                _cache.Delete(key);
                return null;
            }
        }

        public void Delete(string key)
        {
            try
            {
                _cache.Delete(key);
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO DELETE CACHE ITEM [" + key + "]." + e.Message);
            }
        }

        public void Clear()
        {
            _cache.FlushAll();
        }
    }
}
